<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/11/8
 * Time: 10:17
 */
namespace app\admin\controller;
use think\Db;
class Hotel extends Base {

    public function index(){
        if(request()->isAjax()){
            extract(input());

            $map =array();
            if(isset($key)&&$key!="")
            {
                $map['a.h_name'] = ['like',"%" . $key . "%"];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_hotel')
                ->alias ('a')
                ->join('up_hotel_type uht','uht.type_id = a.h_type','left')
                ->where($map)
                ->count();//计算总页面
            $order='a.h_id desc';//排序方式
            $list = Db::table('up_hotel')
                ->alias ('a')
                ->join('up_hotel_type uht','uht.type_id = a.h_type','left')
                ->page($page, $limit)
                ->field('a.*,uht.type_name')
                ->where($map)
                ->order($order)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /***
     * @return mixed
     * 酒店添加
     */
    public function add(){
        if(request()->isAjax()){
            $result['h_name'] = input('param.h_name');
            $result['h_log'] = input('param.images');
            $result['h_cate'] = input('param.h_cate');
            $result['h_level'] = input('param.h_level');
            $result['h_address'] = input('param.h_address');
            $result['h_longitude'] = input('param.longitude');
            $result['h_latitude'] = input('param.latitude');
            $result['h_type'] = input('param.h_type');
            $result['h_wifi'] = input('param.h_wifi');
            $result['h_wried'] = input('param.h_wried');
            $result['h_p'] = input('param.h_p');
            $result['h_phone'] = input('param.h_phone');
            $result['h_min_price'] = input('param.h_min_price');
            $aaa = Db::table('up_hotel')->insertGetId($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $data = Db::table('up_hotel_type')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }


    public function update(){
        if(request()->isAjax()){
            $whereMap['h_id'] = input('param.id');
            $result['h_name'] = input('param.h_name');
            $result['h_log'] = input('param.images');
            $result['h_cate'] = input('param.h_cate');
            $result['h_level'] = input('param.h_level');
            $result['h_address'] = input('param.h_address');
            $result['h_longitude'] = input('param.longitude');
            $result['h_latitude'] = input('param.latitude');
            $result['h_type'] = input('param.h_type');
            $result['h_wifi'] = input('param.h_wifi');
            $result['h_wried'] = input('param.h_wried');
            $result['h_p'] = input('param.h_p');
            $result['h_phone'] = input('param.h_phone');
            $result['h_min_price'] = input('param.h_min_price');
            $aaa = Db::table('up_hotel')->where($whereMap)->update($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }

        $id = input('param.id');
        $where['h_id'] = $id;
        $list = Db::table('up_hotel')
            ->alias ('a')
            ->join('up_hotel_type uht','uht.type_id = a.h_type','left')
            ->field('a.*,uht.type_name')
            ->where($where)
            ->find();
        $data = Db::table('up_hotel_type')->select();
        $this->assign('data',$data);
        $this->assign('res',$list);
        return $this->fetch();
    }

    /**
     * 酒店类型添加
     */
    public function mold(){
        if(request()->isAjax()){
            extract(input());
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_hotel_type')
                ->count();//计算总页面
            $order='type_id desc';//排序方式
            $list = Db::table('up_hotel_type')
                ->page($page, $limit)
                ->order($order)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /***
     * 酒店类型添加
     */
    public function moldadd(){
        if(request()->isAjax()){
            $result['type_name'] = input('param.type_name');
            $aaa = Db::table('up_hotel_type')->insertGetId($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 酒店类型编辑
     */
    public function moldupdate(){
        if(request()->isAjax()){
            $whereMap['type_id'] = input('param.id');
            $result['type_name'] = input('param.type_name');
            $aaa = Db::table('up_hotel_type')->where($whereMap)->update($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $where['type_id'] = $id;
        $data = Db::table('up_hotel_type')->where($where)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /***
     *房间
     */
    public function room(){
        if(request()->isAjax()){
            extract(input());
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_room')
                ->alias('a')
                ->join('up_hotel uh','uh.h_id = a.r_hotel','left')
                ->join('up_room_type urt','urt.type_id = a.r_type','left')
                ->join('up_room_cate urc','urc.c_id = a.r_cate','left')
                ->count();//计算总页面
            $list = Db::table('up_room')
                ->page($page, $limit)
                ->alias('a')
                ->join('up_hotel uh','uh.h_id = a.r_hotel','left')
                ->join('up_room_type urt','urt.type_id = a.r_type','left')
                ->join('up_room_cate urc','urc.c_id = a.r_cate','left')
                ->field('a.*,urt.type_name,urc.cate_name,uh.h_name')
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    public function roomadd(){
        if(request()->isAjax()){
            $result['r_name'] = input('param.h_name');
            $result['r_log'] = input('param.images');
            $result['r_hotel'] = input('param.r_hotel');
            $result['r_num'] = input('param.r_num');
            $result['r_type'] = input('param.r_type');
            $result['r_cate'] = input('param.r_cate');
            $result['r_wifi'] = input('param.r_wifi');
            $result['r_window'] = input('param.r_window');
            $result['r_bathroom'] = input('param.r_bathroom');
            $result['r_breakfast'] = input('param.r_breakfast');
            $result['r_bed'] = input('param.r_bed');
            $result['r_num'] = input('param.r_num');
            $result['r_people'] = input('param.r_people');
            $result['r_cash'] = input('param.r_cash');
            $result['r_rule'] = input('param.r_rule');
            $aaa = Db::table('up_room')->insertGetId($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $data1 = array();
        $data2 = array();
        $data = Db::table('up_hotel')->field('h_name,h_id')->select();
        if(count($data)>0){
            $data1 = Db::table('up_room_cate')->where('hotel_id',$data[0]['h_id'])->select();//房间分类
            $data2 = Db::table('up_room_type')->where('hotel_id',$data[0]['h_id'])->select();//房间类型
        }
        $this->assign('data1',$data1);
        $this->assign('data2',$data2);
        $this->assign('data',$data);
        return  $this->fetch();
    }

    public function roomupdate(){
        if(request()->isAjax()){
            $id = input('param.id');
            $whereMap['r_id'] = $id;
            $result['r_name'] = input('param.h_name');
            $result['r_log'] = input('param.images');
            $result['r_hotel'] = input('param.r_hotel');
            $result['r_num'] = input('param.r_num');
            $result['r_type'] = input('param.r_type');
            $result['r_cate'] = input('param.r_cate');
            $result['r_wifi'] = input('param.r_wifi');
            $result['r_window'] = input('param.r_window');
            $result['r_bathroom'] = input('param.r_bathroom');
            $result['r_breakfast'] = input('param.r_breakfast');
            $result['r_bed'] = input('param.r_bed');
            $result['r_num'] = input('param.r_num');
            $result['r_people'] = input('param.r_people');
            $result['r_cash'] = input('param.r_cash');
            $result['r_rule'] = input('param.r_rule');
            $aaa = Db::table('up_room')->where($whereMap)->update($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $where['a.r_id'] = $id;
        $list = Db::table('up_room')
            ->alias('a')
            ->where($where)
            ->find();
        $data1 = array();
        $data2 = array();
        $data = Db::table('up_hotel')->field('h_name,h_id')->select();
        if(count($data)>0){
            $data1 = Db::table('up_room_cate')->where('hotel_id',$list['r_hotel'])->select();//房间分类
            $data2 = Db::table('up_room_type')->where('hotel_id',$list['r_hotel'])->select();//房间类型
        }
        $this->assign('data1',$data1);
        $this->assign('data2',$data2);
        $this->assign('data',$data);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function ajaxroom(){
        $id = input('param.r_hotel');
        $data1 = Db::table('up_room_cate')->where('hotel_id',$id)->select();//房间分类
        $data2 = Db::table('up_room_type')->where('hotel_id',$id)->select();//房间类型
        return json(['code' => 200, 'data1' =>$data1, 'data2' =>$data2,]);

    }
    /***
     * 房间类型
     */
    public function roomtype(){
        if(request()->isAjax()){
            extract(input());
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_room_cate')
                ->alias('a')
                ->join('up_hotel uh','uh.h_id = a.hotel_id','left')
                ->count();//计算总页面
            $list =  Db::table('up_room_cate')
                ->page($page, $limit)
                ->alias('a')
                ->join('up_hotel uh','uh.h_id = a.hotel_id','left')
                ->field('a.*,uh.h_name')
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 房间分类添加
     */
    public function roomtypeadd(){
        if(request()->isAjax()){
            $result['cate_name'] = input('param.cate_name');
            $result['hotel_id'] = input('param.hotel_id');
            $aaa = Db::table('up_room_cate')->insertGetId($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }

        $data = Db::table('up_hotel')->field('h_name,h_id')->select();

        $this->assign('data',$data);

        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 房间分类
     */
    public function roomtypeupdate(){
        if(request()->isAjax()){
            $whereMap['c_id'] = input('param.id');
            $result['cate_name'] = input('param.cate_name');
            $aaa = Db::table('up_room_cate')->where($whereMap)->update($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_room_cate')->where('c_id',$id)->find();
        $data = Db::table('up_hotel')->field('h_name,h_id')->select();
        $this->assign('data',$data);
        $this->assign('res',$res);
        return $this->fetch();
    }


    /***
     * @return mixed|\think\response\Json
     */
    public function assortment(){
        if(request()->isAjax()){
            extract(input());
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_room_type')
                ->alias('a')
                ->join('up_hotel uh','uh.h_id = a.hotel_id','left')
                ->count();//计算总页面
            $list =  Db::table('up_room_type')
                ->page($page, $limit)
                ->alias('a')
                ->join('up_hotel uh','uh.h_id = a.hotel_id','left')
                ->field('a.*,uh.h_name')
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }


    /***
     * @return mixed|\think\response\Json
     * 分类添加
     */
    public function assortmentadd(){
        if(request()->isAjax()){
            $result['type_name'] = input('param.type_name');
            $result['hotel_id'] = input('param.hotel_id');
            $result['min_price'] = input('param.min_price');
            $aaa = Db::table('up_room_type')->insertGetId($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $data = Db::table('up_hotel')->field('h_name,h_id')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 分类编辑
     */
    public function assortmentupdate(){
        if(request()->isAjax()){
            $whereMap['type_id'] = input('param.id');
            $result['type_name'] = input('param.type_name');
            $result['min_price'] = input('param.min_price');
            $aaa = Db::table('up_room_type')->where($whereMap)->update($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_room_type')->where('type_id',$id)->find();
        $data = Db::table('up_hotel')->field('h_name,h_id')->select();
        $this->assign('data',$data);
        $this->assign('res',$res);
        return $this->fetch();
    }
}